﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using System.Data.SqlClient;
using Code.DAL;
namespace Code.BLL
{
    public class RankManager : ManageBase<Rank>
    {
        public int InsertData(int ran, int exper, double resetMoneyToMy, double resetMoneyToUp)
        {
            int rs = -1;
            using (SqlConnection conn = new SqlConnection(DBHelper.Connection.ConnectionString))
            {
                conn.Open();
                string sql = "insert into Rank(Ranks,Exper,ResetMoneyToMy,ResetMoneyToUp) values(@ran,@exper,@resetMoneyToMy,@resetMoneyToUp)	";
                SqlCommand com = new SqlCommand(sql, conn);
                com.Parameters.AddWithValue("@ran", ran);
                com.Parameters.AddWithValue("@exper", exper);
                com.Parameters.AddWithValue("@resetMoneyToMy", resetMoneyToMy);
                com.Parameters.AddWithValue("@resetMoneyToUp", resetMoneyToUp);
                rs = com.ExecuteNonQuery();

            }


            return rs;
        }

        public int Edit(int id, int ran, int exper, double resetMoneyToMy, double resetMoneyToUp)
        {

            int rs = -1;
            using (SqlConnection conn = new SqlConnection(DBHelper.Connection.ConnectionString))
            {
                conn.Open();
                string sql = "update Rank set Ranks=@ran,Exper=@exper,ResetMoneyToMy=@resetMoneyToMy,ResetMoneyToUp=@resetMoneyToUp where id=@id	";
                SqlCommand com = new SqlCommand(sql, conn);
                com.Parameters.AddWithValue("@id", id);
                com.Parameters.AddWithValue("@ran", ran);
                com.Parameters.AddWithValue("@exper", exper);
                com.Parameters.AddWithValue("@resetMoneyToMy", resetMoneyToMy);
                com.Parameters.AddWithValue("@resetMoneyToUp", resetMoneyToUp);
                rs = com.ExecuteNonQuery();

            }
            return rs;
        }

        public int Del(params int[] ids)
        {
            base.BeginTransaction();

            base.Delete(new { ID = new ComparValue(Comparison.In, ids) });
            int rs = base.ExecuteTransaction();

            return rs;
        }

        public Rank SelectRank()
        {
            Rank rs = null;
            using (SqlConnection conn = new SqlConnection(DBHelper.Connection.ConnectionString))
            {
                conn.Open();
                string sql = "select top 1 * from rank order by id desc";
                SqlCommand com = new SqlCommand(sql, conn);
                SqlDataReader str = com.ExecuteReader();
                if (str.HasRows)
                {
                    rs = new Rank();
                }
                while (str.Read())
                {
                    rs.Ranks = Convert.ToInt32(str["Ranks"]);
                    rs.Exper = Convert.ToInt32(str["Exper"]);
                    rs.ResetMoneyToMy = Convert.ToInt32(str["ResetMoneyToMy"]);
                    rs.ResetMoneyToUp = Convert.ToInt32(str["ResetMoneyToUp"]);
                }

            }
            return rs;
        }
    }
}


